package com.qitianyong.sgkusbcamera;

import android.app.Activity;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.qitianyong.selfclass.FindDeviceThread;
import com.qitianyong.selfclass.LogUtils;
import com.qitianyong.selfclass.UsbDeviceFilter;

public class USBAttachActivity extends Activity {

    private UsbDevice device;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        finish();
        boolean isGKUSBDevice = false;
        Parcelable parcelableExtra = getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);
        if (parcelableExtra instanceof UsbDevice) {
            device = (UsbDevice) parcelableExtra;
            try {
                isGKUSBDevice = UsbDeviceFilter.getMatchingHostDevice(this, R.xml.device_filter, device);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (isGKUSBDevice && TextUtils.equals(getIntent().getAction(), UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
            LogUtils.addLog(getIntent().getAction());
            FindDeviceThread.getInstance().setIntent(FindDeviceThread.OPEN_APP);
        }else {
            LogUtils.addLog("isGKUSBDevice:"+isGKUSBDevice+"  "+getIntent().getAction());
        }
    }
}
